package com.njit.WeStudy.control;

import com.njit.WeStudy.dao.UserMapper;
import com.njit.WeStudy.dataobject.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import static com.njit.WeStudy.util.Check.checkEmail;
import static com.njit.WeStudy.util.Check.checkPhone;
import static com.njit.WeStudy.util.MD5.getMD5String;

/**
 * @author yuruiqiao
 * @date 2020/1/08
 * @description 注册控制类
 */
@Controller
public class signUpController {
    @Resource
    private UserMapper userMapper;

    /**
     * 注册方法,重定向
     * 2020-01-18
     */
    @RequestMapping(path = "/doSignup")
    public String register(User user, HttpSession session, ModelMap modelMap,
                           @RequestParam(value = "emailCode", required = false) String emailCode) {//required=false可以让防止传空值而出错
        modelMap.addAttribute("user", user);
        if (!checkPhone(user.getPhone())) {
            modelMap.addAttribute("phoneMsg", "*手机号格式错误");
            return "signup";
        }
        if (!checkEmail(user.getEmail())) {
            modelMap.addAttribute("emailMsg", "*邮箱格式错误");
            return "signup";
        }
        if (user.getPassword().length() < 6) {
            modelMap.addAttribute("passwordMsg", "*密码不少于6位");
            return "signup";
        }
        if (checkPhone(user.getPhone()) && checkEmail(user.getEmail()) && user.getPassword().length() >= 6) {
            String realEmailCode = (String) session.getAttribute("emailCode");
            //检验邮箱验证码是否正确
            if (emailCode == null || emailCode.equals("")) {
                modelMap.addAttribute("emailCodeMsg", "*邮箱验证码不能为空");
                return "signup";
            } else if (emailCode.equals(realEmailCode)) {
                session.removeAttribute("emailCode");
            } else {
                modelMap.addAttribute("emailCodeMsg", "*邮箱验证码错误");
                return "signup";
            }
            //用于检测已经注册过的电子邮箱、电话号码等
            int emailRepeat = userMapper.selectBy("email", user.getEmail()).size();
            int phoneRepeat = userMapper.selectBy("phone", user.getPhone()).size();
            if (emailRepeat != 0) {
                modelMap.addAttribute("emailMsg", "*已注册");
                return "signup";
            }
            if (phoneRepeat != 0) {
                modelMap.addAttribute("phoneMsg", "*已注册");
                return "signup";
            }
            //MD5加密
            String passwordByMD5 = getMD5String(user.getPassword());
            user.setPassword(passwordByMD5);
            try {
                user.setUserName(user.getEmail());
                user.setUserType("学生");
                user.setHeadImage("/upload/default.png");
                userMapper.insert(user);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "login";
        }
        return "signup";
    }

    @RequestMapping(path = "/signup")
    public String viewSignup(ModelMap modelMap){
        User user = new User();
        modelMap.addAttribute("user",user);
        return "signup";
    }
}
